/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Database;
import model.Diagnose;

/**
 *
 * @author JArik
 */
public class Diagnoses {

    public static int addDiagnose(int diagId, int docId, String descr, Date disDate,
                                  double price, double payed, int visitId,
                                  int dogId, boolean dFin) throws SQLException
    {
        Database db = new Database();
        
        int res=0;
        if(visitId <= 0)
            throw new SQLException("Неправельный номер визита");
        if(docId <= 0)
            throw new SQLException("Неправельный врач");
        if(disDate == null)
            throw new SQLException("Неправельная дата визита");
        if(diagId <=0)
            throw new SQLException("Неправельный номер диагноза");
        if(dogId <= 0)
            throw new SQLException("Неправельный номер животного");

        disDate = new Date(disDate.getTime());
        ResultSet set = db.insertDBInfo("diagnoses_made",
                "visitId,dogId,diagId,exec,descr,date,price,payed,diagStatus",
                ""+visitId+","+dogId+","+diagId+","+docId+",'"+
                descr+"','"+disDate+"',"+price+","+payed+","+
                ( dFin == true ? "0":"-1"));
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        db=null;
        return res;
    }

    public static int updateDiagnose(int disId,
                                     int visitId, int diagId, int docId,
                                     String descr, Date disDate,
                                     double price, double payed,
                                     int dogId, boolean dFin,boolean deleted)
                                     throws SQLException
    {
        Database db = new Database();
        
        if(disId <= 0)
            throw new SQLException("Неправельный идентификатор.");
        if(visitId <= 0)
            throw new SQLException("Неправельный номер визита");
        if(docId <= 0)
            throw new SQLException("Неправельный врач");
        if(disDate == null)
            throw new SQLException("Неправельная дата визита");
        if(diagId <=0)
            throw new SQLException("Неправельный номер диагноза");
        if(dogId <= 0)
            throw new SQLException("Неправельный номер животного");

        disDate = new Date(disDate.getTime());
        int res = db.updateDBInfo("diagnoses_made",
                "visitId="+visitId+","+
                "dogId="+dogId+","+
                "diagId="+diagId+","+
                "exec="+docId+","+
                "descr='"+descr+"',"+
                "date='"+disDate+"',"+
                "price="+price+","+
                "payed="+payed+","+
                "diagStatus="+(dFin == true ? "0":"-1")+","+
                "deleted="+(deleted== true ? "1":"0"),
                // WHERE
                " disId =" +disId);

        db.closeStatement();
        db=null;
        return res;
    }


    public static int addToCatalog(Diagnose diag) throws SQLException {
        Database db = new Database();
        int res=0;
        if(diag== null || (diag.getName().isEmpty() && diag.getRusName().isEmpty()))
            throw new SQLException("Введите название диагноза");
        ResultSet set = db.insertDBInfo("diagnoses",
                "name,rusName", "'"+diag.getName() +"','"+diag.getRusName()+"'");
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        return res;
    }
    public static int getIdFromName(String name) throws SQLException
    {
        Database db = new Database();
        int res=0;
        ResultSet set = db.getDBInfo("id", "diagnoses",
                "name ='"+name+"'");
        while(set.next())
        {
            res= set.getInt(1);
        }
        db.closeStatement();
        return res;
    }

    public static ArrayList<Diagnose> getByName(String name) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo("id, name, rusName", "diagnoses",
                "(name LIKE '%"+name+"%'  OR rusName LIKE '%"+name+"%') AND deleted=0 " +
                "ORDER BY name != '"+name+"' OR rusName != '"+name+"'," +
                "         name not like '"+name+" %'," +
                "         name not like '"+name+"%', name  ");
        ArrayList<Diagnose> list = new ArrayList<Diagnose>();
        while ( set.next())
        {
            Diagnose diag = new Diagnose();
            diag.setId(set.getInt(1));
            diag.setName(set.getString(2));
            diag.setRusName(set.getString(3));
            list.add(diag);
        }
        return list;
    }
}
 